<?php

include 'common.php';

//后台管理首页
//后台action动作
$action = !empty($_GET['action']) ? $_GET['action'] : '';
//Admin模型
//1.能判断用户名密码是否正确  login
//2.未登录用户不能访问某些页面的功能函数 identifyLoggedin
$AdminModel = new AdminModel();
//Category模型
//获取所有的分类
$CategoryModel = new CategoryModel();
//根据传递的action参数，执行对应的操作
switch ($action) {
    //登录
    case 'login':
//        var_dump($_POST);
        //假如是POST提交，才处理
        if (!empty($_POST)) {
            //获取POST提交的用户名
            $username = $_POST['username'];
            //获取POST提交的密码
            $password = $_POST['password'];
            //开始验证

            $row = $AdminModel->login($username, $password);
            if ($row) {
                echo "<script>alert('登录成功!')</script>";
                header("Location:admin.php?action=index");
            } else {
                echo "<script>alert('登录失败!')</script>";
                //sleep(1);
                include "tpl/admin/error.php";
                //header("Location:admin.php?action=login");
            }
            die();
        }
        include 'tpl/admin/login.php';
        break;
    //管理首页
    case 'index':
        $AdminModel->identifyLoggedin();
        include 'tpl/admin/index.php';
        break;
    //日志列表
    case 'article':
        //实例化日志模型
        //获取所有日志列表，暂不分页。
        $ArticleModel = new ArticleModel();
        $list = $ArticleModel->getAllItems();
//        var_dump($list);
        include 'tpl/admin/article.php';
        break;
    //发表日志
    case 'articleadd':
        //如果提交了数据，就保存
        //如果没有提交数据，就显示我们的FORM表单。
        if (!empty($_POST)) {
            //这里就是我们处理提交的数据的过程。
//            var_dump($_POST);
            //拼装提交的数据
            $data = array(
                'title' => $_POST['title'],
                'content' => $_POST['content'],
                'catid' => intval($_POST['catid']),
                'addtime' => date("Y-m-d H:i:s")
            );
            $ArticleModel = new ArticleModel();
            $row = $ArticleModel->addArticle($data);
            if ($row) {
                echo "<script>alert('恭喜发布成功!');location.href='admin.php?action=article'</script>";
                die();
            } else {
                echo "<script>alert('发布失败,请重回发布！');location.href='admin.php?action=articleadd'</script>";
                die();
            }
//            $ArticleModel = new ArticleModel();
//            $row = $ArticleModel->addArticle($data);
            var_dump($data);
            die();
        }
        include 'tpl/admin/articleadd.php';
        break;
    //日志编辑
    case 'articleedit':
        $id = (int)$_GET['id'];
        $ArticleModel = new ArticleModel();
        $detail = $ArticleModel->getDetailById($id);
        include 'tpl/admin/articleedit.php';
        break;
    //日志分类
    case 'category':
        $list = $CategoryModel->getAllItems();
        include 'tpl/admin/category.php';
        break;

//    删除日志
    case 'delete':
        $id = (int)$_GET['id'];
        $ArticleModel = new ArticleModel();
        $details = $ArticleModel->delete($id);
        header("Location:admin.php?action=article");
        include 'tpl/admin/delete.php';
    //编辑分类
    case "editCategory":

    //删除分类
    case "deleteCategory":

    default :
}

